<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 订单模型
 */
class Order_model extends CI_Model{

	public function __construct()
	{
		parent::__construct();
	}
	
	public function offer_order_by_id($id)
	{
		$sql = "SELECT  * FROM pc_ddth_offer_order WHERE id = $id";
	
		$query = $this->db->query($sql);
	
		return $query->row_array();
	}
	public function accept_order_by_id($id)
	{
		$sql = "SELECT  * FROM pc_ddth_accept_order WHERE id = $id";
	
		$query = $this->db->query($sql);
	
		return $query->row_array();
	}

	/**
	 * 提供帮助订单插入
	 */
	public function offer_order_insert($user_id,$money,$bianhao)
	{
		$data['user_id'] = $user_id;

		$data['money'] = $money;

		$data['sy_money'] = $money;

		$data['create_time'] = time();

		$data['bianhao'] = $bianhao;

		$result = $this->db->insert('pc_ddth_offer_order', $data);

		return $result;
	}

	/**
	 * 上级接收下级的单子后插入的帮助订单
	 */
	public function offer_order_new_insert($user_id,$money,$bianhao,$old_id)
	{
		$data['user_id'] = $user_id;

		$data['money'] = $money;

		$data['sy_money'] = 0;

		$data['pipei_time'] = time();

		$data['status'] = 1;

		$data['create_time'] = time();

		$data['bianhao'] = $bianhao;

		$data['old_offer_id'] = $old_id;

		$result = $this->db->insert('pc_ddth_offer_order', $data);

		return $result;
	}

	/**
	 * 防止重复插入
	 */
	public function offer_order_has($user_id,$old_id)
	{

		$sql = "SELECT count(id) AS num FROM pc_ddth_offer_order WHERE user_id = $user_id AND old_offer_id = $old_id ";

		$query = $this->db->query($sql);

		return $query->row_array();
	}


	/**
	 * 提供帮组订单列表
	 */
	public function offer_order_list()
	{
		$sql = "SELECT  * FROM pc_ddth_offer_order WHERE status = 0";

		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * 按id获取订单
	 */
	public function offer_order_list_id($uid)
	{
		$sql = "SELECT  * FROM pc_ddth_offer_order WHERE user_id = $uid";

		$query = $this->db->query($sql);

		return $query->result_array();
	}

	public function offer_order_row_id($uid)
	{
		$sql = "SELECT  * FROM pc_ddth_offer_order WHERE user_id = $uid AND status = 0";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 更新提供帮助订单剩余金额
	 */
	public function offer_order_symoney_update($money,$id)
	{
		$data['sy_money'] = $money;

		$data['pipei_time'] = time();

		//无剩余金额
		if($money == 0)
		{
			$data['status'] = 1;
		}

		$result = $this->db->update('pc_ddth_offer_order', $data,array('id' =>$id));

		return $result;

	}

	/**
	 * 提供帮助的的订单全部匹配并已打款
	 */
	public function offer_order_pay_on($id)
	{
		$data['pay_status'] = 1;

		$data['pay_time'] = time();

		$result = $this->db->update('pc_ddth_offer_order', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 提供帮助的的订单拒绝打款
	 */
	public function offer_order_pay_off($id)
	{
		$data['pay_status'] = 2;

		$data['pay_time'] = time();

		$result = $this->db->update('pc_ddth_offer_order', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 接受帮助订单
	 * $order_type 0 静态钱包，1推广钱包
	 */
	public function accept_order_insert($user_id,$order_type,$money)
	{
		$data['user_id'] = $user_id;

		$data['order_type'] = $order_type;

		$data['money'] = $money;

		$data['sy_money'] = $money;

		$data['create_time'] = time();

		$result = $this->db->insert('pc_ddth_accept_order', $data);

		return $result;
	}

	/**
	 * 更新获取帮助订单剩余金额
	 */
	public function accept_order_symoney_update($money,$id)
	{
		$data['sy_money'] = $money;
		//无剩余金额
		if($money == 0)
		{
			$data['status'] = 1;
		}

		$result = $this->db->update('pc_ddth_accept_order', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 帮助提现订单列表
	 */
	public function accept_order_list()
	{
		$sql = "SELECT  * FROM pc_ddth_accept_order WHERE status = 0";

		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * 按id获取订单
	 */
	public function accept_order_list_id($uid)
	{
		$sql = "SELECT  * FROM pc_ddth_accept_order WHERE user_id = $uid  ORDER BY id DESC";
		//return $sql;
		$query = $this->db->query($sql);

		return $query->result_array();
	}

	public function accept_order_row_id($uid)
	{
		$sql = "SELECT  * FROM pc_ddth_accept_order WHERE user_id = $uid AND status = 0 ORDER BY id DESC";
		//return $sql;
		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 
	 *type = offer or type = accept
	 */
	public function pipei_order_list($type)
	{
		if($type == 'offer')
		{
			$table_name = 'pc_ddth_offer_order';
		}
		else
		{
			$table_name = 'pc_ddth_accept_order';
		}

		$sql = "SELECT * FROM " . $table_name . " WHERE status = 0 AND sy_money <> 0";

		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * 订单日志插入
	 */
	public function order_log_insert($oid,$aid,$money)
	{
		$data['offer_order_id'] = $oid;

		$data['accept_order_id'] = $aid;

		$data['money'] = $money;

		$data['create_time'] = time();

		$result = $this->db->insert('pc_ddth_order_log',$data);

		return $result;
	}

	/**
	 * 订单日志列表
	 * $type==0:$order_id提供帮助订单号，$type==1:$order_id获取帮助订单号
	 *
	 */
	public function order_log_list($type,$order_id)
	{
		$sql = " SELECT * FROM pc_ddth_order_log WHERE 1=1 ";

		if($type == 0)
		{
			$sql = $sql ." AND offer_order_id = $order_id";
		}
		else
		{
			$sql = $sql ." AND accept_order_id = $order_id";
		}
		//echo $sql;
		$query = $this->db->query($sql);

		return $query->result_array();
	}


	/**
	 * 根据accept的user_id获取order_log的列表
	 */
	public function accept_order_log_list_uid($uid)
	{
		$sql = "SELECT ";
		$sql .= "ao.`create_time` AS sq_time,";
		$sql .= "ao.`order_type`,";
		$sql .= "ol.id,";
		$sql .= "oo.bianhao,";
		$sql .= "ol.money,";
		$sql .= "ol.`create_time` AS pp_time ,";
		$sql .= "oo.`user_id`,";
		$sql .= "dakuan,dakuan_time,";
		$sql .= "shoukuan,";
		$sql .= "img_url, ";
		$sql .= "user_name, ";
		$sql .= "user_account, ";
		$sql .= "mobile ";
		$sql .= "FROM `pc_ddth_accept_order` AS ao,`pc_ddth_order_log` AS ol,`pc_ddth_offer_order` AS oo ,pc_ddth_user AS u ";
		$sql .= "WHERE ol.`accept_order_id` = ao.`id` ";
		$sql .= "AND ol.`offer_order_id` = oo.`id` ";
		$sql .= "AND (ol.`dakuan` <> '1' ";
		$sql .= "OR ol.`shoukuan` <> '1') ";
		$sql .= "AND oo.user_id = u.`id` ";
		$sql .= "AND ao.`user_id` = " . $uid;
		$sql .= " ORDER BY ol.id";
		$query = $this->db->query($sql);
		//echo $sql;
		return $query->result_array();
	}

	/**
	 * 根据offer的user_id获取order_log的列表
	 */
	public function offer_order_log_list_uid($uid)
	{
		$sql = "SELECT ";
		$sql .= "oo.`create_time` AS sq_time,";
		$sql .= "ol.id,";
		$sql .= "oo.bianhao,";
		$sql .= "ol.money,";
		$sql .= "ol.day_num,";
		//$sql .= "ol.lilv,";
		$sql .= "ol.out_day_num,";
		$sql .= "ol.`create_time` AS hz_time ,";
		$sql .= "oo.`user_id`,";
		$sql .= "dakuan,";
		$sql .= "img_url, ";
		$sql .= "user_name, ";
		$sql .= "user_account, ";
		$sql .= "zfb_account, ";
		$sql .= "bank_name, ";
		$sql .= "bank_card, ";
		$sql .= "wx_id, ";
		$sql .= "mobile,";
		$sql .= "up_account ";
		$sql .= "FROM `pc_ddth_accept_order` AS ao,`pc_ddth_order_log` AS ol,`pc_ddth_offer_order` AS oo ,pc_ddth_user AS u ";
		$sql .= "WHERE ol.`accept_order_id` = ao.`id` ";
		$sql .= "AND ol.`offer_order_id` = oo.`id` ";
		$sql .= "AND ao.user_id = u.`id` ";
		$sql .= "AND oo.`user_id` = " . $uid;
		$sql .= " ORDER BY ol.id";
		//echo $sql;
		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * 根据id获取日志表信息
	 */
	public function get_order_log_id($id)
	{
		$sql = "SELECT * FROM pc_ddth_order_log WHERE id = $id";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 按用户获取个人未出场日志
	 */
	public function get_order_log_chuchang($user_id)
	{
		$sql = "SELECT ol.* FROM `pc_ddth_order_log` ol,`pc_ddth_accept_order` ao WHERE ol.`accept_order_id` = ao.id AND out_status = 0 AND out_day_num <> 0 AND ao.user_id = $user_id";

		$query = $this->db->query($sql);

		return $query->result_array();

	}

	/**
	 * 更新订单日志出场状态
	 */
	public function order_log_update_chuchang($ol_id)
	{
		$data['out_status'] = 1;

		$result = $this->db->update('pc_ddth_order_log', $data,array('id' =>$ol_id));

		return $result;
	}

	/**
	 * 订单日志打款状态更新
	 */
	public function update_order_log_dakuan($status,$id)
	{
		$data['dakuan'] = $status;

		$data['dakuan_time'] = time();

		$result = $this->db->update('pc_ddth_order_log', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 收款
	 */
	public function update_order_log_shoukuan($status,$id)
	{
		$data['shoukuan'] = $status;

		$data['shoukuan_time'] = time();
		
		$result = $this->db->update('pc_ddth_order_log', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 日志表更新出场日期
	 */
	public function order_log_update_day_num($day_num,$id)
	{
		$data['out_day_num'] = $day_num;

		$result = $this->db->update('pc_ddth_order_log', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 更新图片URL
	 */
	public function order_log_update_img($img_url,$id)
	{
		$data['img_url'] = $img_url;

		$result = $this->db->update('pc_ddth_order_log', $data,array('id' =>$id));

		return $result;
	}

	/**
	 * 出场记录插入
	 */
	public function out_log_insert($data)
	{
		$data['create_time'] = time();

		$result = $this->db->insert('pc_ddth_out_log',$data);

		return $result;
	}

	/**
	 * 根据订单日志id判断入场记录是否存在
	 */
	public function get_out_log($ol_id)
	{
		$sql = "SELECT * FROM pc_ddth_out_log WHERE ol_id = $ol_id";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 根据订单id和用户id判断团队分红记录是否存在
	 */
	public function get_team_log($user_id,$offer_order_id)
	{
		$sql = "SELECT * FROM pc_ddth_team_log WHERE user_id = $user_id AND offer_order_id = $offer_order_id";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 获取当日分红金额总和
	 */
	public function get_team_log_day_money($user_id)
	{
		$sql = "SELECT COALESCE(SUM(money),0) AS sum_money FROM `pc_ddth_team_log` ";
		$sql .= "WHERE TO_DAYS(FROM_UNIXTIME(create_time)) = TO_DAYS(NOW()) AND user_id = $user_id";

		$query = $this->db->query($sql);

		$row = $query->row_array();
		
		return $row['sum_money'];

	}

	/**
	 * 获取未打款订单
	 */
	public function get_order_dakuan($user_id)
	{
		$sql = "SELECT count(ol.id) as num FROM ";
		$sql .="pc_ddth_order_log AS ol,pc_ddth_offer_order AS oo ";
		$sql .="WHERE oo.id = ol.offer_order_id AND oo.user_id = $user_id AND ol.dakuan = 0";

		$query = $this->db->query($sql);

		$row = $query->row_array();

		return $row['num'];
	}

	/**
	 * 获取未收款订单
	 */
	public function get_order_shoukuan($user_id)
	{
		$sql = "SELECT count(ol.id) as num FROM ";
		$sql .="pc_ddth_order_log AS ol,pc_ddth_accept_order AS ao ";
		$sql .="WHERE ao.id = ol.accept_order_id AND ao.user_id = $user_id AND ol.shoukuan = 0";

		$query = $this->db->query($sql);

		$row = $query->row_array();

		return $row['num'];
	}

	/**
	 * 获取团队记录
	 */
	public function get_up_account_offer_list_num($path)
	{
		$sql = "SELECT count(o.id) AS num FROM pc_ddth_offer_order as o,pc_ddth_user as u,pc_ddth_user as upu WHERE o.user_id = u.id AND u.path LIKE '{$path}%' AND upu.user_account = u.up_account";
		//echo $sql;
		$query = $this->db->query($sql);

		$row = $query->row_array();

		return $row['num'];
	}

	/**
	 * 团队记录分页
	 */
	public function get_up_account_offer_list($page,$perpage,$path)
	{
		$sql = "SELECT oo.*,u.user_name,u.user_account,u.up_account ,upu.user_name as up_name ";
		$sql .= "FROM pc_ddth_offer_order AS oo,pc_ddth_user AS u ,pc_ddth_user AS upu ";
		$sql .= "WHERE u.path LIKE '{$path}%' AND u.id = oo.user_id AND u.`up_account` = upu.`user_account` limit ".$perpage*($page-1).','.$perpage;
		//echo $sql;
		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * 获取用户最新的提供帮助订单
	 */
	public function get_new_offer_order($user_id)
	{
		$sql = "SELECT id,user_id,`money` FROM pc_ddth_offer_order WHERE user_id = $user_id ORDER BY id DESC LIMIT 1";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 提供帮助的已打款金额
	 */
	public function get_offer_order_dakuan_money($offer_order_id)
	{
		$sql = "SELECT oo.id,oo.money,SUM(ol.`money`) AS sum_money ";
		$sql .= "FROM `pc_ddth_offer_order` oo ,`pc_ddth_order_log` ol ";
		$sql .= "WHERE oo.id = ol.`offer_order_id` AND ol.`dakuan` = 1 AND offer_order_id = $offer_order_id";

		$query = $this->db->query($sql);

		return $query->row_array();
	}

	/**
	 * 上级拒绝或接受订单的信息更新
	 */
	public function offer_order_on_off_userid($id,$user_id)
	{
		$data['up_user_id'] = $user_id;

		$result = $this->db->update('pc_ddth_offer_order', $data,array('id' =>$id));

		return $result;
	}
}
